/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wangxiandeng.swipecardrecyclerview.slice;

import com.wangxiandeng.swipecardrecyclerview.ResourceTable;
import com.wangxiandeng.swipecardrecyclerview.SwipeCardRecyclerView;
import com.wangxiandeng.swipecardrecyclerview.adapter.BaseAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    private BaseAdapter mProvider;
    private ArrayList<String> al=new ArrayList<>();
    private int i;
    Text text;
    SwipeCardRecyclerView swipeFlingAdapterView;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        text= (Text) findComponentById(ResourceTable.Id_test);


        swipeFlingAdapterView= (SwipeCardRecyclerView) findComponentById(ResourceTable.Id_frame);
        swipeFlingAdapterView.setIsCardOverlap(false);
        for (int i = 0; i < 11; i++) {
            al.add(String.valueOf(i));
        }
        mProvider = new BaseAdapter(this);
        mProvider.setData(al);
        swipeFlingAdapterView.setItemProvider(mProvider);
        swipeFlingAdapterView.setFlingListener(new SwipeCardRecyclerView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                al.remove(0);
                mProvider.notifyDataSetItemRemoved(0);
            }

            @Override
            public void onLeftCardExit(Object dataObject) {

                text.setText("Right！"+dataObject.toString());

            }

            @Override
            public void onRightCardExit(Object dataObject) {
                text.setText("Left！"+dataObject.toString());
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {

            }

            @Override
            public void onScroll(float scrollProgressPercent) {

            }
        });

    }

    /**
     * item component holder
     */
    public static class MyComponentHolder {
        private final Component itemView;

        public MyComponentHolder(Component itemView) {
            if (itemView == null) {
                throw new IllegalArgumentException("itemView may not be null");
            } else {
                this.itemView = itemView;
            }
        }

        public Component getItemView() {
            return itemView;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
